package thread;

public class Demo12 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            //获取线程引用
            Thread currentThread = Thread.currentThread();
            while (!currentThread.isInterrupted()) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    //throw new RuntimeException(e);
                    System.out.println("执行catch操作...");
                    break;
                }
            }
        });

        t1.start();

        Thread.sleep(3000);
        //在主线程中，控制t线程终止，设置上述标志位，还可以唤醒sleep
        //需要注意的是，interrupt将sleep等阻塞的函数唤醒后，会清空刚才的设置的interrupt的标志位
        t1.interrupt();
    }
}
